<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/1
 * Time: 15:02
 */

namespace app\common\model;


use think\Db;
use think\Model;

class Sourcecodem extends Model
{
    /**
     * 源码管理列表
     */
    public static function sourceCodeList($map = [], $page)
    {
        return Db::name('source_code')->alias('sc')
            ->join('user', 'u_id = sc_uid', 'left')
            ->where($map)
            ->field('sc.*, u_nickname')
            ->order('sc_id DESC')
            ->paginate(15, false, ['page' => $page]);
    }

    /**审核源码
     * @param $id 源码ID
     * @param $status 源码状态
     * @param $uid 用户ID
     * @param sctitle  源码标题
     * @return bool
     */
    public static function editSourceCode($id, $status, $uid, $sctitle)
    {
//        var_dump($id."++++".$status."++++".$uid."++++".$sctitle);die;
        if($status == 3){//审核通过
            $u_integral = Db::name('systems')->where('s_id', 1)->value('s_source_code');//源码审核通过后  获得的积分
            Db::startTrans();
            try{
                //源码审核通过
                Db::name('source_code')->where('sc_id', $id)->update(['sc_status' => $status, 'sc_pass_time' => time()]);
                Db::name('user')->where('u_id', $uid)->setInc('u_integral',$u_integral);
                $title   = '源码审核通过';
                $content = '恭喜您，源码:《'.$sctitle.'》审核通过。获得'.$u_integral.'积分。';
                Messagem::addUserMessage($uid, $title, $content, 2);
                Db::commit();
                return true;
            } catch(\Exception $e) {
                Db::rollback();
                return false;
            }
        } elseif ($status == 4) {
            Db::startTrans();
            try{
                //源码审核不通过
                Db::name('source_code')->where('sc_id', $id)->update(['sc_status' => $status, 'sc_not_pass_time' => time()]);
                $title   = '源码审核失败';
                $content = '很抱歉您的源码:《'.$sctitle.'》审核未通过，请重新提交您要发布的源码';
                Messagem::addUserMessage($uid, $title, $content, 4);
                Db::commit();
                return true;
            } catch(\Exception $e) {
                Db::rollback();
                return false;
            }
        } else {
            return false;
        }
    }
    /**
     *根据源码ID 获取源码的详情
     */
    public static function detailSourceCode($id)
    {
        return Db::name('source_code')->alias('sc')
            ->join('user', 'u_id = sc_uid', 'left')
            ->where('sc_id', $id)
            ->field('sc.*, u_nickname')
            ->find();
    }

    /**
     *获取目前所有置顶的源码
     */
    public static function getRoofSourceCode()
    {
        return  Db::name('source_code')->where('sc_is_top', 2)->where('sc_top_end_time', '<', time())->select();
    }
    /**
     *  取消过期置顶源码 置顶的定时器方法
     */
    public static function cancelRoofSourceCode($data)
    {
        Db::startTrans();
        try {
            Db::name('source_code')->where('sc_id', $data['sc_id'])->update(['sc_is_top' => 1]);
            $title   = '源码置顶过期';
            $content = '您于'.date('Y-m-d', $data['sc_top_start_time']).'置顶的，'.$data['sc_title'].'源码已过期！';
            Messagem::addUserMessage($data['sc_uid'], $title, $content, 4);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }
}